//
//  UIView+Corner.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/30.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

extension UIView {
    func yoko_addCorner(withSize sizeToFit: CGSize, radius: CGFloat) {
        let imageView = UIImageView(image:  yoko_drawRectWithRoundedCorner(withSize: sizeToFit, radius: radius))
        self.insertSubview(imageView, atIndex: 0)
    }
    
    func yoko_drawRectWithRoundedCorner(withSize sizeToFit: CGSize, radius: CGFloat) -> UIImage {
        let rect = CGRectMake(0, 0, sizeToFit.width, sizeToFit.height)
        UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.mainScreen().scale)
        let context = UIGraphicsGetCurrentContext()
    
//    CGContextMoveToPoint(context, 开始位置);  // 开始坐标右边开始
//    CGContextAddArcToPoint(context, x1, y1, x2, y2, radius);  // 这种类型的代码重复四次
        CGContextAddPath(context, UIBezierPath.init(roundedRect: rect, cornerRadius: radius).CGPath)
        CGContextClip(context)
        
        self.drawRect(rect)
//        CGContextDrawPath(UIGraphicsGetCurrentContext(), .FillStroke)
        let output = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return output
    }
}
